/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import componenten.Database;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.Deelnemer;
import model.Lid;
import model.Masterclass;

/**
 *
 * @author daveronvanderham
 */
public class MasterclassBeheren extends javax.swing.JFrame {
    
    private int masterclass_id;

    /**
     * Creates new form MasterclassBeheren
     */
    public MasterclassBeheren(int masterclass_id) {
        initComponents();
        this.masterclass_id = masterclass_id;
        Masterclass Bmasterclass = new Masterclass(masterclass_id);
        masterclass.setText(Integer.toString(Bmasterclass.getMasterclass_id()));
        Master.setText(Bmasterclass.gerMasterBijnaam(Bmasterclass.getMaster()));
        Datum.setText(Bmasterclass.getDatum());
        Tijd.setText(Bmasterclass.getTijd());
        Deelnemers.setText(Integer.toString(Bmasterclass.getMin_deelnemers()));
        Inleggeld.setText(Double.toString(Bmasterclass.getInleggeld()));
        Rating.setText(Bmasterclass.getMin_rating());
        vulTabelDeelnemer(masterclass_id);
        vulTabelleden(masterclass_id);
    }
    
    private void vulTabelDeelnemer(int masterclass_id) {
        //Ophalen van de toernooien uit de database

        Deelnemer deelnemer = new Deelnemer();
        String[] kolommen = {"lid","email" ,"betaald?"};
        DefaultTableModel model = new DefaultTableModel(kolommen, 0);
        
        int aantalLocaties = deelnemer.haalAantalLocatiepOp(masterclass_id);

        //Lijstje aanmaken voor de tennistoernooien die we uit de database gaan halen. 
        Deelnemer[] deelnemers;
        if (aantalLocaties == -1) {
            //return null;
        } else {
            deelnemers = new Deelnemer[aantalLocaties];
        }
        
        try {
            
            String sql = "SELECT d.lid_id , p.persoon_email , d.deelnemer_betaalstatus FROM deelnemer d join persoon p on d.lid_id = p.persoon_id WHERE masterclass_id = " + masterclass_id + "";
            ResultSet resultaat = Database.executeSelectQuery(sql);
            
            int teller = 0;
            while (resultaat.next()) {
                
                int dlnmr = resultaat.getInt("d.lid_id");
                String email = resultaat.getString("p.persoon_email");
                String btlng = resultaat.getString("d.deelnemer_betaalstatus");
                
                Deelnemer tabelVulling = new Deelnemer(dlnmr, this.masterclass_id, email , btlng);
                model.addRow(tabelVulling.getInformatie());
            }
            
            
        } catch (SQLException e) {
        }
        
        deelnemerTable.setModel(model);
        
    }
    
    private void vulTabelleden(int masterclass_id) {
        //Ophalen van de toernooien uit de database

        Lid lid = new Lid();
        String[] kolommen = {"id", "email", "rating"};
        DefaultTableModel model = new DefaultTableModel(kolommen, 0);
        
        int aantalLocaties = lid.haalAantalLedenOp();

        //Lijstje aanmaken voor de tennistoernooien die we uit de database gaan halen. 
        Lid[] leden;
        if (aantalLocaties == -1) {
            //return null;
        } else {
            leden = new Lid[aantalLocaties];
        }
        
        try {
            
            String sql = "select l.persoon_id , l.lid_rating,p.persoon_email from lid l join persoon p on l.persoon_id = p.persoon_id where l.persoon_id not in (SELECT lid_id FROM deelnemer WHERE masterclass_id = " + masterclass_id + ")";
            ResultSet resultaat = Database.executeSelectQuery(sql);
            
            int teller = 0;
            while (resultaat.next()) {
                
                int lid_id = resultaat.getInt("persoon_id");
                String email = resultaat.getString("persoon_email");
                int rating = resultaat.getInt("lid_rating");
                
                Lid tabelVulling = new Lid(lid_id, email, rating);
                model.addRow(tabelVulling.getInformatie());
                
                
                
            }
            
            
        } catch (SQLException e) {
        }
        
        ledenTable.setModel(model);
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        deelnemerTable = new javax.swing.JTable();
        Master = new javax.swing.JTextField();
        Datum = new javax.swing.JTextField();
        Tijd = new javax.swing.JTextField();
        Deelnemers = new javax.swing.JTextField();
        deelnemerToevoegen = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        Inleggeld = new javax.swing.JTextField();
        masterclass = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        Rating = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        cancel = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        ledenTable = new javax.swing.JTable();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        deelnemerTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        deelnemerTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                deelnemerTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(deelnemerTable);

        Datum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DatumActionPerformed(evt);
            }
        });

        deelnemerToevoegen.setText("<");
        deelnemerToevoegen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deelnemerToevoegenActionPerformed(evt);
            }
        });

        jButton3.setText("opzeggen");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel1.setText("master");

        jLabel2.setText("datum");

        jLabel3.setText("tijd");

        jLabel4.setText("min deelnemers");

        jLabel5.setText("inleggeld");

        jLabel6.setText("masterclass id");

        jLabel7.setText("minimale rating");

        cancel.setText("cancel");
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        ledenTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(ledenTable);

        jLabel8.setText("deelnemers");

        jLabel9.setText("leden");

        jButton1.setText("voltooien");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jLabel4)
                            .add(jLabel3)
                            .add(jLabel2)
                            .add(jLabel1)
                            .add(jLabel5)
                            .add(jLabel6)
                            .add(jLabel7))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 107, Short.MAX_VALUE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(Master)
                            .add(Datum)
                            .add(Tijd)
                            .add(Deelnemers)
                            .add(Inleggeld)
                            .add(masterclass)
                            .add(Rating, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 81, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(34, 34, 34)
                                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 330, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createSequentialGroup()
                                .add(126, 126, 126)
                                .add(jLabel8)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(deelnemerToevoegen, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 293, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(jLabel9)
                                .add(100, 100, 100))))
                    .add(layout.createSequentialGroup()
                        .add(9, 9, 9)
                        .add(jButton1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton3)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cancel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 114, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(77, 77, 77)
                        .add(deelnemerToevoegen))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(jLabel8)
                                .add(3, 3, 3)
                                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 232, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                        .add(masterclass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .add(jLabel6))
                                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel9))
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(layout.createSequentialGroup()
                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                            .add(Master, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                            .add(jLabel1))
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                            .add(Datum, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                            .add(jLabel2))
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                            .add(Tijd, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                            .add(jLabel3))
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                            .add(Deelnemers, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                            .add(jLabel4))
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                            .add(jLabel5)
                                            .add(Inleggeld, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                            .add(Rating, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                            .add(jLabel7)))
                                    .add(layout.createSequentialGroup()
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 161, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
                        .add(13, 13, 13)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButton3)
                            .add(cancel)
                            .add(jButton1))))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void DatumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DatumActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_DatumActionPerformed
    
    private void deelnemerToevoegenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deelnemerToevoegenActionPerformed
        
        int row = ledenTable.getSelectedRow();
        String lid = (String) ledenTable.getModel().getValueAt(row, 0);
        Deelnemer nieuwDeelnemer = new Deelnemer(this.masterclass_id, lid);
        
        if (nieuwDeelnemer.addDeelnemer()) {
            JOptionPane.showMessageDialog(null, "deelnemer toegevoegd");
            vulTabelDeelnemer(masterclass_id);
            vulTabelleden(masterclass_id);
            
        } else {
            System.out.println("masterclass toevoegen: opslaan: " + "error bij opslaan");
        }
        
    }//GEN-LAST:event_deelnemerToevoegenActionPerformed
    
    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        this.dispose();
        new MasterclassOverzicht().setVisible(true);
    }//GEN-LAST:event_cancelActionPerformed
    
    private void deelnemerTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deelnemerTableMouseClicked
        // TODO add your handling code here:
        int row = deelnemerTable.getSelectedRow();
        String deelnemer = (String) deelnemerTable.getModel().getValueAt(row, 0);
        String email = (String) deelnemerTable.getModel().getValueAt(row, 1);
        new DeelnemerScherm(Integer.parseInt(deelnemer), email,this.masterclass_id).setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_deelnemerTableMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        Masterclass voltooiMasterclass = new Masterclass(masterclass_id);
        voltooiMasterclass.setStatus("voltooid");
        voltooiMasterclass.update();
        JOptionPane.showMessageDialog(null, "masterclass is voltooid");
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        Masterclass updateMasterclass = new Masterclass(masterclass_id);
        updateMasterclass.setStatus("inactief");
        updateMasterclass.update();
        JOptionPane.showMessageDialog(null, "masterclass is opgezegd");
    }//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MasterclassBeheren.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MasterclassBeheren.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MasterclassBeheren.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MasterclassBeheren.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {
            
            public void run() {
                new MasterclassBeheren(1).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField Datum;
    private javax.swing.JTextField Deelnemers;
    private javax.swing.JTextField Inleggeld;
    private javax.swing.JTextField Master;
    private javax.swing.JTextField Rating;
    private javax.swing.JTextField Tijd;
    private javax.swing.JButton cancel;
    private javax.swing.JTable deelnemerTable;
    private javax.swing.JButton deelnemerToevoegen;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable ledenTable;
    private javax.swing.JTextField masterclass;
    // End of variables declaration//GEN-END:variables
}
